package com.ruoyi.web.controller.oms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oms.entity.OrderItem;
import com.ruoyi.oms.service.IOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 订单明细Controller
 *
 * @author ruoyi
 * @date 2021-11-02
 */
@RestController
@RequestMapping("/oms/item")
public class OmsOrderItemController extends BaseController {
    @Autowired
    private IOrderItemService omsOrderItemService;

    /**
     * 查询订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderItem omsOrderItem) {
        startPage();
        List<OrderItem> list = omsOrderItemService.list(new QueryWrapper<>(omsOrderItem));
        return getDataTable(list);
    }

    /**
     * 导出订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:item:export')")
    @Log(title = "订单明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OrderItem omsOrderItem) {
        List<OrderItem> list = omsOrderItemService.list(new QueryWrapper<>(omsOrderItem));
        ExcelUtil<OrderItem> util = new ExcelUtil<OrderItem>(OrderItem.class);
        return util.exportExcel(list, "订单明细数据");
    }

    /**
     * 获取订单明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(omsOrderItemService.getById(id));
    }

    /**
     * 新增订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:item:add')")
    @Log(title = "订单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderItem omsOrderItem) {
        return toAjax(omsOrderItemService.save(omsOrderItem));
    }

    /**
     * 修改订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:item:edit')")
    @Log(title = "订单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderItem omsOrderItem) {
        return toAjax(omsOrderItemService.updateById(omsOrderItem));
    }

    /**
     * 删除订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:item:remove')")
    @Log(title = "订单明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        return toAjax(omsOrderItemService.removeByIds(list));
    }
}